<template>
  <el-dialog
    title="添加图书"
    v-model="show"
    width="30%"
    center
    :show-close="false"
  >
    <el-form :model="book">
      <el-form-item label="图书分类">
        <el-radio-group v-model="book.category2">
          <el-radio label="文学" />
          <el-radio label="艺术" />
          <el-radio label="历史" />
          <el-radio label="科技" />
          <el-radio label="儿童" />
          <el-radio label="教材" />
        </el-radio-group>
      </el-form-item>
      <el-form-item label="图书标题">
        <el-input v-model="book.title2" />
      </el-form-item>
      <el-form-item label="图书价格">
        <el-input v-model="book.price2" />
      </el-form-item>
      <el-form-item label="图片地址">
        <el-input v-model="book.book_img2" placeholder="请在网上复制图片链接" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="qu">取消</el-button>
        <el-button type="primary" @click="submitForm">确认</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
<script setup>
import { ref, reactive } from 'vue'
import emitter from '../utils/eventBus'
import { addBook } from '../api/index'
import { ElMessage } from 'element-plus'

// 表单数据
const book = reactive({
  title2: '',
  price2: '',
  book_img2: '',
  category2: '',
})

// 表单验证
const submitForm = async () => {
  const res = await addBook(book)
  // 如果修改成功
  if (res.status === 200) {
    ElMessage({
      message: '添加成功！！！',
      type: 'success',
    })
    show.value = false
    book.title2 = ''
    book.price2 = ''
    book.book_img2 = ''
    book.category2 = ''
  }
}

// 控制添加图书对话框的显示与隐藏
const show = ref(false)
emitter.on('add', (flag) => {
  show.value = flag
})
const qu = () => {
  show.value = false
}
</script>
<style lang="less" scoped>
.dialog-footer button:first-child {
  margin-right: 10px;
}

:deep(.el-form-item__label) {
  width: 80px !important;
}

:deep(.el-form-item__content) {
  justify-content: center;
  margin-left: 0 !important;
}
</style>
